import Layout from '@/layout'
function iteratorRoute(iteratorRoute,parentUrl){
    var result = [];
    iteratorRoute.forEach((child,idx) =>{
        var routers = {
            path: child.menuUrl===null?'':child.menuUrl,
            name: child.menuCode,
            hidden: child.hidden == 'true',
            meta: { id: child.id, title: child.menuName, icon: child.menuIcon ,cache: child.cache=="true"}
        }
        if(child.children.length>0){
            routers.component = Layout
            routers.children = iteratorRoute(child.children,child.menuUrl) 
        }else {                     // resolve => require([`@/views/${child.menuUrl}`], resolve),
            const url = parentUrl+child.menuUrl
            routers.component = resolve => require([`@/views${url}`], resolve)
        }
        result[idx] = routers
    })
    return result;
  }
  
export function processRoute(asyncRoutes){
    var array = [];
    if(asyncRoutes.length >0){
        asyncRoutes.forEach((value ,index) => {
            if(value.children.length>0){
                let route = {
                    path: value.menuUrl===null?'':value.menuUrl,
                    name: value.menuCode,
                    component:Layout,
                    redirect: value.menuUrl+value.children[0].menuUrl,
                    meta: { title: value.menuName, icon: value.menuIcon ,id: value.id,cache: value.cache=="true" }
                  }
                  if(value.children.length ==1){
                    route.alwaysShow = true
                  }
                route.children = iteratorRoute(value.children,value.menuUrl);
                array[index] = route;
            }else {
                array[index] = 
                {
                    path: value.menuUrl.substring(0,value.menuUrl.indexOf('/',1)),
                    component:Layout,
                    redirect: value.menuUrl.substring(1),
                    children: [{
                        path: value.menuUrl.substring(value.menuUrl.indexOf('/',1)),
                        hidden: value.hidden == 'true',
                        name: value.menuCode, // `@/${value.menuUrl}.vue`
                        component: resolve => require([`@/views${value.menuUrl}`], resolve),
                        meta: { id: value.id,title: value.menuName, icon: value.menuIcon,cache: value.cache=="true"   }
                    }]
                    
                }
            }
        });
    }   
    return array;
  }